﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KylinORM.CQRS.Publisher;

namespace KylinORM.CQRS.Subscriber
{
    /// <summary>
    /// 消息响应明细
    /// </summary>
    public class MessageHandlerItem
    {
        #region 初始化
        public MessageHandlerItem()
        {
            this.MessageLevel = EMessageLevel.Normal;
        }

        /// <summary>
        /// 创建消息响应明细
        /// </summary>
        /// <param name="callbackFunc"></param>
        public MessageHandlerItem(Delegate callbackFunc)
            : this()
        {
            this.CallbackFunc = callbackFunc;
        }
        /// <summary>
        /// 创建消息响应明细
        /// </summary>
        /// <param name="callbackFunc"></param>
        /// <param name="messageType"></param>
        public MessageHandlerItem(Delegate callbackFunc, EMessageType messageType)
            : this(callbackFunc)
        {
            this.MessageType = messageType;
        }

        /// <summary>
        /// 创建消息响应明细并指定优先级
        /// </summary>
        /// <param name="callbackFunc"></param>
        /// <param name="messageLevel"></param>
        public MessageHandlerItem(Delegate callbackFunc, EMessageLevel messageLevel)
            : this(callbackFunc)
        {
            this.MessageLevel = messageLevel;
        }
        /// <summary>
        /// 创建消息响应明细并指定优先级
        /// </summary>
        /// <param name="callbackFunc"></param>
        /// <param name="messageType"></param>
        /// <param name="messageLevel"></param>
        public MessageHandlerItem(Delegate callbackFunc, EMessageType messageType, EMessageLevel messageLevel)
            : this(callbackFunc, messageLevel)
        {
            this.MessageType = messageType;
        }

        /// <summary>
        /// 创建消息响应明细并指定依赖条件
        /// </summary>
        /// <param name="callbackFunc"></param>
        /// <param name="handlerItems"></param>
        public MessageHandlerItem(Delegate callbackFunc, params MessageHandlerItem[] handlerItems)
            : this(callbackFunc)
        {
            this.DependsOn(handlerItems);
        }
        /// <summary>
        /// 创建消息响应明细并指定依赖条件
        /// </summary>
        /// <param name="callbackFunc"></param>
        /// <param name="messageType"></param>
        /// <param name="handlerItems"></param>
        public MessageHandlerItem(Delegate callbackFunc, EMessageType messageType, params MessageHandlerItem[] handlerItems)
            : this(callbackFunc,handlerItems)
        {
            this.MessageType = messageType;
        }
        #endregion

        /// <summary>
        /// 消息响应的回调方法
        /// </summary>
        public Delegate CallbackFunc { get; set; }
        /// <summary>
        /// 消息响应的类型，值为NULL时使用命令类型
        /// </summary>
        public EMessageType? MessageType { get; set; }
        /// <summary>
        /// 消息响应的优先级
        /// </summary>
        public EMessageLevel MessageLevel { get; set; }
        /// <summary>
        /// 消息响应的依赖条件
        /// </summary>
        /// <param name="handlerItems"></param>
        public MessageHandlerItem DependsOn(params MessageHandlerItem[] handlerItems)
        {
            this.dependsOn.AddRange(handlerItems);
            return this;
        }
        internal List<MessageHandlerItem> dependsOn = new List<MessageHandlerItem>();

    }
}
